package T2;

import java.util.Random;
import java.util.Scanner;

// 定义水果枚举类
enum Fruit {
    APPLE("苹果", 2),
    PEAR("鸭梨", 5),
    BANANA("香蕉", 8),
    GRAPE("葡萄", 10);

    private String name;
    private int betCoins;

    Fruit(String name, int betCoins) {
        this.name = name;
        this.betCoins = betCoins;
    }

    public String getName() {
        return name;
    }

    public int getBetCoins() {
        return betCoins;
    }
}

// 定义游戏类
class FruitGuessGame {
    private int coins = 10;
    private Scanner scanner = new Scanner(System.in);
    private Random random = new Random();

    // 打印游戏开始界面
    private void printStartInterface() {
        System.out.println("-----------------------------");
        System.out.println("         猜水果游戏         ");
        System.out.println("-----------------------------");
        System.out.println("         游戏规则         ");
        System.out.println("本游戏练习选择、循环结构的内容。");
        System.out.println("游戏者现拥有10枚金币，但手中的");
        System.out.println("金币数量少于10枚时无法继续游戏而");
        System.out.println("自行退出。");
        System.out.println("每次下注一种水果，猜对了赢取相应数量的金币，猜错了就失去相应数量的金币。");
        System.out.println("-----------------------------");
        System.out.println("基础财富：10枚金币");
    }

    // 打印下注界面
    private void printBetInterface() {
        System.out.println("请选择下注的水果：");
        for (Fruit fruit : Fruit.values()) {
            System.out.println((fruit.ordinal() + 1) + "、" + fruit.getName() + "------ " + fruit.getBetCoins() + "枚金币");
        }
        System.out.println("0、退出游戏");
    }

    public void startGame() {
        printStartInterface();
        while (coins >= 10) {
            printBetInterface();
            int choice = 0;

            // 验证输入是否为数字
            while (!scanner.hasNextInt()) {
                System.out.println("请输入有效的数字！");
                scanner.next(); // 清除无效输入
            }
            choice = scanner.nextInt();

            // 处理退出选项
            if (choice == 0) {
                System.out.println("游戏结束，感谢参与！");
                break;
            }

            if (choice < 1 || choice > Fruit.values().length) {
                System.out.println("无效选择，请重新输入。");
                continue;
            }

            Fruit betFruit = Fruit.values()[choice - 1];
            if (coins < betFruit.getBetCoins()) {
                System.out.println("金币不足，无法下注。");
                continue;
            }

            Fruit targetFruit = getRandomFruit();
            System.out.println("本次随机水果是：" + targetFruit.getName());

            if (betFruit == targetFruit) {
                coins += betFruit.getBetCoins();
                System.out.println("恭喜，猜对了！当前金币数量：" + coins);
            } else {
                coins -= betFruit.getBetCoins();
                System.out.println("很遗憾，猜错了！当前金币数量：" + coins);
            }
        }

        if (coins < 10) {
            System.out.println("金币不足10枚，游戏结束。");
        }

        scanner.close(); // 关闭Scanner资源
    }

    private Fruit getRandomFruit() {
        int index = random.nextInt(Fruit.values().length);
        return Fruit.values()[index];
    }
}

class Main {
    public static void main(String[] args) {
        FruitGuessGame game = new FruitGuessGame();
        game.startGame();
    }
}
